/*
 * SimpleContent.java
 *
 * Created on 20 marzo 2004, 10.24
 */

package net.ermannofranco.xml.schema;

import java.util.List;

/**
 * 
 * @author ermanno (ermanno.franco@gmail.com)
 */
public class SimpleContent extends SchemaTag implements BaseTypeContainer {
	private BaseTypeContainer baseContainer;

	/**
	 * 
	 */
	private static final long serialVersionUID = -782037973440545435L;
	private boolean assigned;

	/**
	 * Creates a new instance of SimpleContent Protetto
	 * 
	 * @param level
	 *            livello di indentazione
	 * @param container
	 *            tag contenitore
	 */
	protected SimpleContent(int level, ISchemaTag container) {
		super("simpleContent", level, container);
	}

	/**
	 * aggiunge un tag Annotation che contiene un tag Documentation
	 * 
	 * @param explanation
	 *            Stringa di documentazione
	 * @return autoriferimento all'oggetto corrente
	 */
	public SimpleContent addDocumentation(String explanation) {
		addAnnotation().addDocumentation(explanation);
		return this;
	}

	/**
	 * aggiunge un tag Annotation che contiene un tag Documentation
	 * 
	 * @param explanation
	 *            testo della documentazione
	 * @param language
	 *            attributo di xml:lang, seguire le regole di i18n
	 * @return riferimento all'oggetto corrente
	 */
	public SimpleContent addDocumentation(String explanation, String language) {
		addAnnotation().addDocumentation(explanation, language);
		return this;
	}

	/** */
	private ExtensionSimple setExtension() {
		if (assigned)
			throw new SchemaException(
					"Extension non permessa, contenuto gia' assegnato");
		ExtensionSimple e;
		add(e = new ExtensionSimple(getLevel() + 1, this));
		assigned = true;
		return e;
	}

	/**
	 * @param baseType
	 *            costante di XmlType.
	 * @return oggetto Extension
	 */
	public ExtensionSimple setExtension(XmlType baseType) {
		ExtensionSimple base = setExtension().setBase(baseType);
		this.baseContainer = baseType;
		return base;
	}

	/**
	 * @param typeRef
	 * @return oggetto Extension
	 */
	// public Extension setExtension(String typeRef) {
	// return setExtension().setBase(typeRef);
	// }

	/**
	 * @param typeRef
	 * @return oggetto Extension
	 */
	public ExtensionSimple setExtension(ContentType typeRef) {
		ExtensionSimple base = setExtension().setBase(typeRef);
		baseContainer = typeRef;
		return base;
	}

	/**
	 * semplice override di convalida
	 * 
	 * @return super.toString()
	 */
	public String toPrint() {
		if (!assigned)
			throw new SchemaException("Assegnare contenuto a simpleContent");
		return super.toPrint();
	}

	public String toSave() {
		if (!assigned)
			throw new SchemaException("Assegnare contenuto a simpleContent");
		return super.toSave();
	}

	private RestrictionSimpleContent setRestriction() {
		RestrictionSimpleContent re;
		if (assigned)
			throw new SchemaException(
					"Restriction non permessa, contenuto gia' assegnato");
		add(re = new RestrictionSimpleContent(getLevel() + 1, this));
		assigned = true;
		return re;
	}

	public RestrictionSimpleContent setRestriction(XmlType type) {
		RestrictionSimpleContent re = setRestriction().setBase(type);
		baseContainer = re;
		return re;
	}

	// public RestrictionSimpleContent setRestriction(String typeName) {
	// return setRestriction().setBase(typeName);
	// }

	public RestrictionSimpleContent setRestriction(ContentType typeRef) {
		RestrictionSimpleContent re = setRestriction().setBase(typeRef);
		baseContainer = typeRef;
		return re;
	}

	public List<XmlType> getBaseTypes() {
		return baseContainer.getBaseTypes();
	}
}
